/**
 * 
 */
package com.meks.crmi.app;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.FlushModeType;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.persist.jpa.JpaPersistModule;
import com.meks.crmi.entities.SparePart;
import com.meks.crmi.service.SparePartService;

/**
 * main (launching) class of the application.
 * @author mhager
 *
 */
public final class CrmInvoicingApp {
	
	private Log logger = LogFactory.getLog(getClass()); 
	
	/**
	 * hidden constructor because this class is just the entry point of the
	 * application, so just the main method is valid for usage.
	 */
	private CrmInvoicingApp() {
		
	}

	/**
	 * @param args 
	 */
	public static void main(final String[] args) {
		System.setProperty("derby.system.home", "/home/mhager/.crm-invoicing/db/derby");
		new CrmInvoicingApp().run();
	}
	
	private void run() {
		logger.info("starting crm-invoicing");
		Injector injector = Guice.createInjector(new LocalDependencieSetup(), new JpaPersistModule("irmInvoiceDb"));
		injector.getInstance(AppInitialzier.class);
		SparePartService service = injector.getInstance(SparePartService.class);
		List<SparePart> sparePartList = service.getAllSpareParts();
		
		System.out.println("found " + sparePartList.size() + " spare parts");
		injector.getInstance(EntityManager.class).setFlushMode(FlushModeType.COMMIT);
		for (int i=0; i<10;i++) {
			SparePart part = new SparePart();
			part.setName("TestName " + i);
			service.createSparePart(part);
		}
		injector.getInstance(AppInitialzier.class).shutdown(injector.getInstance(EntityManager.class));
	}

}
